package cst.neu.yuejian_31.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import cst.neu.yuejian_31.Common.Constant;
import cst.neu.yuejian_31.R;
import cst.neu.yuejian_31.bean.AddorderBean;
import cst.neu.yuejian_31.bean.UserInfoBean;
import cst.neu.yuejian_31.iface.AddorderListener;
import cst.neu.yuejian_31.iface.UserInfoListener;
import cst.neu.yuejian_31.model.AddorderModel;
import cst.neu.yuejian_31.model.UserInfoModel;

public class ActivityOrderActivity extends BaseActivity implements UserInfoListener,AddorderListener {
        private TextView userName,activityMoney,activityaddress,activityName,userTel,toTal,ordernum;
        private ImageView add,resort,activityPic,btn_return;
        private Button btn_order;
        private String userId,activityId,price;
        private int num,sum;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_activityorder);
            initViews();
            initDates();
            initEvent();
        }

        private void initEvent() {
            add.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    num=Integer.parseInt(ordernum.getText().toString());
                    num++;
                    ordernum.setText(String.valueOf(num));
                    sum=Integer.parseInt(activityMoney.getText().toString())*Integer.parseInt(ordernum.getText().toString());
                    toTal.setText(String.valueOf(sum));
                }
            });
            resort.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    num=Integer.parseInt(ordernum.getText().toString());
                    if(num>1){
                        num--;
                        ordernum.setText(String.valueOf(num));
                        sum=Integer.parseInt(activityMoney.getText().toString())*Integer.parseInt(ordernum.getText().toString());
                        toTal.setText(String.valueOf(sum));
                    }
                }
            });
            btn_order.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    doOrder();
                }
            });
            btn_return.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });

        }

        private void doOrder(){
            AddorderModel orderModel=new AddorderModel();
            orderModel.addOrdera(userId,activityId,String.valueOf(num),String.valueOf(sum),this);

        }

        private void initDates() {
            Intent intent=getIntent();
            activityId=intent.getStringExtra("activity_id");
            activityName.setText(intent.getStringExtra("activity_name"));
            activityMoney.setText(intent.getStringExtra("activity_money"));
            activityaddress.setText(intent.getStringExtra("activity_address"));
            toTal.setText(intent.getStringExtra("activity_money"));
            Picasso.with(ActivityOrderActivity.this).load(Constant.ACTIVITY_URL+intent.getStringExtra("activity_pic").toString()).into(activityPic);

            userId=getUserId();
            UserInfoModel userInfoModel=new UserInfoModel();
            userInfoModel.getUserInfo(userId,this);

        }

        private void initViews() {
            activityName= (TextView) findViewById(R.id.textView_activity_order_name);
            activityMoney= (TextView) findViewById(R.id.textview_train_order_money);
            activityaddress= (TextView) findViewById(R.id.textView_train_order_address);
            activityPic= (ImageView) findViewById(R.id.imageView_activity_order_pic);
            userName= (TextView) findViewById(R.id.textView_train_order_username);
            userTel=(TextView)findViewById(R.id.textView_activity_order_userphone);
            add= (ImageView) findViewById(R.id.imageView_order_add);
            resort= (ImageView) findViewById(R.id.imageView_order_resort);
            toTal= (TextView) findViewById(R.id.textView_train_order_total);
            ordernum= (TextView) findViewById(R.id.textView_order_num);
            btn_order= (Button) findViewById(R.id.button_doorder);
            btn_return= (ImageView) findViewById(R.id.imageView_train_order_return);
        }

        @Override
        public void onResponse(UserInfoBean user) {
            userName.setText(user.getUsername());
            userTel.setText(user.getUserphone());

        }

        @Override
        public void onSuccess(AddorderBean addorderBean) {
            Toast.makeText(this, "下单成功", Toast.LENGTH_SHORT).show();

        }

        @Override
        public void onFail(String msg) {

        }
    }
